/*
 * This file is part of the Android Dimensions Editor Project: http://code.google.com/p/android-dimension-editor/
 *  
 * Copyright (c) 2013 Rick Falck
 *  
 * This code is licensed under the terms of the Apache License Version 2.0.
 * You may use this code according to the license.
 *
 * The terms of the license can be found in the root directory of this project's repository as well as at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under these Licenses is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See each License for the specific language governing permissions and
 * limitations under that License.
 */
package application;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import model.DimensFile;
import model.ItemComment;
import model.ItemName;
import model.ItemValue;

public class DimensFile_XML_RW {

    private static final String xmlVersion = "1.0";
    private static final String encoding = "utf-8";
    private static final String rootElement = "resources";
    private static final String dimenElement = "dimen";
    private static final String dimenAttribute = "name";

    public static ArrayList<ItemName> readFile(File file) throws FileNotFoundException,
            XMLStreamException {
        // create the XMLInputFactory object
        ArrayList<ItemName> nameValueMap = new ArrayList<>();
        XMLInputFactory inputFactory = XMLInputFactory.newFactory();

        // create a XMLStreamReader object
        FileInputStream fis = new FileInputStream(file);
        XMLStreamReader reader = inputFactory.createXMLStreamReader(fis);

        // read the Ingredients from the file
        while (reader.hasNext()) {
            int eventType = reader.getEventType();
            switch (eventType) {
                case XMLStreamConstants.START_ELEMENT:
                    String elementName = reader.getLocalName();
                    switch (elementName) {
                        case rootElement:
                            break;
                        case dimenElement:
                            ItemName name = new ItemName(reader.getAttributeValue(0));
                            ItemValue value = new ItemValue(reader.getElementText(), true);
                            name.setMyValue(value);
                            nameValueMap.add(name);
                            break;
                        default:
                            throw new XMLStreamException("Invalid Element Found: <" + elementName
                                    + ">");
                    }
                    break;
                case XMLStreamConstants.COMMENT:
                    ItemComment comment = new ItemComment(reader.getText().trim());
                    ItemValue value = new ItemValue(DimensFile.COMMENT_VALUE, false);
                    comment.setMyValue(value);
                    nameValueMap.add(comment);
                default:
                    break;
            }
            reader.next();
        }

        return nameValueMap;

    }

    public static void writeFile(File file, ArrayList<ItemName> nameValueMap)
            throws IOException, XMLStreamException {
        XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();

        // create XMLStreamWriter object
        //FileWriter fileWriter = new FileWriter(file);
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileOutputStream(file), "utf-8");

        // write the products to the file
        writer.writeStartDocument(encoding, xmlVersion);
        writer.writeCharacters("\n");
        writer.writeStartElement(rootElement);

        for (ItemName name : nameValueMap) {
            writer.writeCharacters("\n    ");
            if (name.isComment()) {
                writer.writeComment(name.getText());
            } else {
                ItemValue value = name.getMyValue();
                writer.writeStartElement(dimenElement);
                writer.writeAttribute(dimenAttribute, name.getText().trim());
                writer.writeCharacters(value.getText().trim());
                writer.writeEndElement();
            }
        }
        writer.writeCharacters("\n");
        writer.writeEndElement(); // rootElement
        writer.flush();
        writer.close();
    }
}
